package com.lonely.fx.dialog;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.Optional;
import java.util.function.Consumer;

/**
 * @date: 2022-08-01 17:00
 * @author: ztkj-hzb
 * @description: 可输入的对话框 属性设置
 **/
public class TextInputDialogSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        Button button = new Button("点击打开弹框");
        hBox.getChildren().addAll(button);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                TextInputDialog textInputDialog = new TextInputDialog();

                //设置header
                textInputDialog.setHeaderText("请输入你的名字");

                //获取输入框对象
                TextField editor = textInputDialog.getEditor();

                Optional<String> optional = textInputDialog.showAndWait();

                optional.ifPresent(new Consumer<String>() {
                    @Override
                    public void accept(String s) {
                        System.out.println("输入的内容：" + s);
                    }
                });

            }
        });

    }
}
